package com.song.nio;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.Socket;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NIO {
    public void test() {
        try {
            RandomAccessFile aFile = new RandomAccessFile(new File("C:\\Users\\Administrator\\Desktop\\job51.txt"), "rw");

            FileChannel inChannel = aFile.getChannel();
            ByteBuffer buf = ByteBuffer.allocate(48);
            int read = inChannel.read(buf);
            System.out.println("========");
            while (read != -1) {
                System.out.println("read == " + read);
                buf.flip();
                while (buf.hasRemaining()) {
                    System.out.print((char) buf.get());
                }
                buf.clear();
                read = inChannel.read(buf);
            }
            inChannel.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {

        NIO nio = new NIO();
        nio.test();
    }
    public void test2(){
        Socket socket = new Socket();
    }

}
